package com.humble.vichatbackend.exception;

import com.humble.vichatbackend.common.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(BusinessException.class)
    public BaseResponse businessExceptionHandler(BusinessException e){
        log.error("出现已定义错误,错误码：{},错误信息：{},错误描述：{}",e.getCode(),e.getMessage(),e.getDescription());
        return BaseResponse.fail(e.getCode(),e.getMessage(),e.getDescription());
    }

    @ExceptionHandler(RuntimeException.class)
    public BaseResponse runtimeExceptionHandler(RuntimeException e){
        log.error("出现未知错误,错误信息：{}",e.getMessage());
        return BaseResponse.fail(50000,"系统内部错误："+e.getMessage());
    }
}
